import { USER_LOGIN_SUCCESS, USER_LOGIN_FAIL, ActionType } from './userAction'

interface State {
	isLogin: boolean,
	token: string
	nickname: string,
	signation:string,
	user: string,
	role: string[]
}

const initState: State = {
	isLogin: false,
	token: localStorage.getItem('token') || '',
	nickname: '',
	signation: '',
	user: '',
	role: []
}



const reducer = (state = initState, action: ActionType) => {
	switch(action.type) {
		case USER_LOGIN_SUCCESS:
			localStorage.setItem('token',action.payload.token)
			return {
				...state,
				...action.payload,
				isLogin: true
			}
		case USER_LOGIN_FAIL: {
			localStorage.removeItem('token')
			return {
				isLogin: false,
				token: '',
				nickname: '',
				signation: '',
				user: '',
				role: ''
			}
		}
		default :
			return state
	}
}

export default reducer